package userlimitController

import (
	"arabApi/app/api/controllers"
	"arabApi/app/api/controllers/CFun"
	"arabApi/app/api/requests"
	"arabApi/app/api/requests/CRequest"
	"arabApi/app/api/requests/userlimitRequest"
	"arabApi/app/api/services/systemConfigService"
	"arabApi/app/api/services/userLimitService"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/spf13/cast"
)

type User struct {
	controllers.BaseAPIController
}

// @Summary 获取 用户列表
// @title 后台接口
// @Tags 用户
// @Router /v1/userList [get]
// @param param query requests.PageStruct true "用户请求参数"
// @Success 200 {object} CFun.DetailInfo
func (User) List(c *gin.Context) {

	r := requests.PageStruct{}
	if err := c.ShouldBindQuery(&r); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("解析错误"+err.Error()))
		return
	}

	list, count := userLimitService.UserList(r)
	data := gin.H{}
	data["list"] = list
	data["count"] = count
	CFun.Ajax_return_success(c, CFun.WithData(data))
}

// @Summary 获取 添加用户
// @title 后台接口
// @Tags 用户
// @Router /v1/userAdd [post]
// @param param body userlimitRequest.UserAdd true "用户请求参数"
// @Success 200 {object}  CFun.DetailInfo
func (User) Add(c *gin.Context) {

	r := userlimitRequest.UserAdd{}
	if err := c.ShouldBindBodyWith(&r, binding.JSON); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("解析错误"+err.Error()))
		return
	}

	if ok := requests.Validate(c, &r, userlimitRequest.CheckUserAdd); !ok {
		return
	}

	if ok := userLimitService.UserAdd(r); ok == 0 {
		CFun.Ajax_return_fail(c, CFun.WithMsg("添加失败"))
	} else {
		CFun.Ajax_return_success(c, CFun.WithMsg("添加成功"))
	}

}

// @Summary 获取 编辑用户
// @title 后台接口
// @Tags 用户
// @Router /v1/userEdit [post]
// @param param body userlimitRequest.UserAdd true "用户请求参数"
// @Success 200 {object}  CFun.DetailInfo
func (User) Edit(c *gin.Context) {
	r := userlimitRequest.UserAdd{}
	if err := c.ShouldBindBodyWith(&r, binding.JSON); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("解析错误"+err.Error()))
		return
	}

	if ok := requests.Validate(c, &r, userlimitRequest.CheckUserAdd); !ok {
		return
	}

	if ok := userLimitService.UserEdit(r); ok != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("编辑失败"+ok.Error()))
	} else {
		CFun.Ajax_return_success(c, CFun.WithMsg("编辑成功"))
	}
}

// @Summary 获取 用户信息
// @title 后台接口
// @Tags 用户
// @Router /v1/userMsg [get]
// @param param query CRequest.Oid true "用户请求参数"
// @Success 200 {object}  CFun.DetailInfo
func (User) MSg(c *gin.Context) {

	request := CRequest.Oid{}

	// 解析 JSON 请求
	if err := c.ShouldBindQuery(&request); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg(err.Error()))
		return
	}

	if ok := requests.Validate(c, &request, CRequest.CheckID); !ok {
		return
	}

	system := systemConfigService.GetMsgById(cast.ToInt64(request.Id))
	CFun.Ajax_return_success(c, CFun.WithData(system))

}

func (User) UserRoleMenu(c *gin.Context) {
	request := struct {
		ID int `json:"uid" form:"uid"`
	}{}

	// 解析 JSON 请求
	if err := c.ShouldBindQuery(&request); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg(err.Error()))
		return
	}

	list := userLimitService.UserMenu(cast.ToString(request.ID))

	CFun.Ajax_return_success(c, CFun.WithData(list))
}
